package gao.xiaolei.quertz;

import gao.xiaolei.util.ServiceName;
import lombok.extern.slf4j.Slf4j;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import java.time.LocalDateTime;

//用于定时将测试内容保存为副本的任务
@Slf4j
@DisallowConcurrentExecution
public class CopyWorkJob implements Job {

    @Autowired
    private RestTemplate restTemplate;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        log.info("------------------------------进入到复制任务的job类,时间：".concat(LocalDateTime.now().toString()));
        JobDataMap dataMap = jobExecutionContext.getJobDetail().getJobDataMap();
        String project = jobExecutionContext.getJobDetail().getKey().getGroup();
        MultiValueMap<String, Object> params = new LinkedMultiValueMap<>(2);
        params.add("type", dataMap.getInt("type"));
        params.add("project", project);
        HttpEntity httpEntity = new HttpEntity<>(params);
        restTemplate.postForEntity(ServiceName.TEST_SERVICE.getServiceName().concat("/copy"), httpEntity, String.class);
        log.info("------------------------------退出复制任务的job类,时间：".concat(LocalDateTime.now().toString()));
    }
}
